package me.zhengjie.modules.website.service.impl;

import me.zhengjie.modules.website.domain.CmInfos;
import me.zhengjie.modules.website.repository.CmInfosRepository;
import me.zhengjie.modules.website.repository.CmInfosSpecifications;
import me.zhengjie.modules.website.service.CmInfosService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import me.zhengjie.modules.website.domain.PageResult;

import java.util.List;

/**
 * @author system
 * @since 2025-01-14 17:56:04
 */
@Service
public class CmInfosServiceImpl implements CmInfosService {

    @Autowired
    private CmInfosRepository cmInfosRepository;

    @Override
    public PageResult<CmInfos> findPage(CmInfos cmInfos, Pageable pageable) {
        Page<CmInfos> page = cmInfosRepository.findAll(CmInfosSpecifications.queryList(cmInfos), pageable);
        PageResult<CmInfos> result = new PageResult();
        result.setContent(page.getContent());
        result.setTotalElements(page.getTotalElements());
        return result;
    }

    @Override
    public List<CmInfos> findList(CmInfos cmInfos) {
        return cmInfosRepository.findAll(CmInfosSpecifications.queryList(cmInfos));
    }

    @Override
    public CmInfos findById(Long id) {
        return cmInfosRepository.findById(id).orElse(null);
    }

    @Override
    public CmInfos insert(CmInfos cmInfos) {
        return cmInfosRepository.save(cmInfos);
    }

    @Override
    public CmInfos update(CmInfos cmInfos) {
        return cmInfosRepository.saveAndFlush(cmInfos);
    }

    @Override
    public void delete(Long id) {
        cmInfosRepository.deleteById(id);
    }

    @Override
    public Page<CmInfos> search(String infoTitle, String infoType, Pageable pageable) {
        return cmInfosRepository.findByConditions(infoTitle, infoType, pageable);
    }
}
